# -*- coding: UTF-8 -*-
"""
@auth:buxiangjie
@date:2023/2/2 15:46
@describe: 模糊规则断言
"""
import traceback

from assertpy.assertpy import assert_that
from jsonpath import jsonpath
from util.common import FuzzCommon

import json


class RequiredAssert:
	def __call__(self, *args, **kwargs) -> None:
		try:
			_fail_basic(*args, **kwargs)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


class NoRequiredAssert:
	def __call__(self, *args, **kwargs) -> None:
		try:
			_pass_basic(*args, **kwargs)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


class DateTimeNoFormatAssert:
	def __call__(self, *args, **kwargs):
		try:
			_fail_basic(*args, **kwargs)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


class Int32EqualsLeftBorderAssert:
	def __call__(self, *args, **kwargs) -> None:
		try:
			_pass_basic(*args, **kwargs)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


class Int32EqualsRightBorderAssert:
	def __call__(self, *args, **kwargs) -> None:
		try:
			_pass_basic(*args, **kwargs)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


class Int32BeyondLeftBorderAssert:
	def __call__(self, *args, **kwargs) -> None:
		try:
			_fail_basic(*args, **kwargs)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


class Int32BeyondRightBorderAssert:
	def __call__(self, *args, **kwargs) -> None:
		try:
			_fail_basic(*args, **kwargs)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


class Int64EqualsLeftBorderAssert:
	def __call__(self, *args, **kwargs) -> None:
		try:
			_pass_basic(*args, **kwargs)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


class Int64EqualsRightBorderAssert:
	def __call__(self, *args, **kwargs) -> None:
		try:
			_pass_basic(*args, **kwargs)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


class Int64BeyondLeftBorderAssert:
	def __call__(self, *args, **kwargs) -> None:
		try:
			_fail_basic(*args, **kwargs)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


class Int64BeyondRightBorderAssert:
	def __call__(self, *args, **kwargs) -> None:
		try:
			_fail_basic(*args, **kwargs)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


class RemoveHeadersAssert:
	def __call__(self, *args, **kwargs) -> None:
		try:
			assert_that(kwargs["code"], "断言HTTP CODE是否正确").is_greater_than_or_equal_to(400)
			assert_that(kwargs["code"], "断言HTTP CODE是否正确").is_less_than(500)
			assert_that(kwargs["code"], "断言HTTP CODE是否正确").is_equal_to(404)
		except AssertionError as e:
			return e
		except TypeError as e:
			return e
		except Exception:
			print(kwargs)
			raise OSError(traceback.extract_stack())


def _pass_basic(*args, **kwargs) -> None:
	assert_that(kwargs["code"], "断言HTTP CODE是否正确").is_equal_to(200)
	assert_that(_get_result_code(kwargs["req_response"]), "断言resultCode是否正确").is_in(2000, 4000)
	# if _get_result_code(kwargs["req_response"]) == 4000:
	# 	assert_that(_get_errmsg(kwargs["req_response"]), "断言errMsg是否正确").is_equal_to("进件未找到")


def _fail_basic(*args, **kwargs) -> None:
	assert_that(kwargs["code"], "断言HTTP CODE是否正确").is_equal_to(200)
	assert_that(_get_result_code(kwargs["req_response"]), "断言resultCode是否正确").is_equal_to(4000)


def _rep_json(rep: str) -> dict:
	try:
		reps = json.loads(rep)
		return reps
	except Exception as e:
		raise e


def _get_result_code(rep: str) -> int:
	return jsonpath(_rep_json(rep), "$...resultCode")[0]


def _get_errmsg(rep: str) -> str:
	return jsonpath(_rep_json(rep), "$...errMsg")[0]
